// internal/domain/network.go
package domain

import (
	"context"
	"time"
)

// NetworkService 虚拟网络服务接口
type NetworkService interface {
	// 网络管理
	ListNetworks(ctx context.Context, connID string) ([]*NetworkInfo, error)
	GetNetwork(ctx context.Context, connID, networkName string) (*NetworkInfo, error)
	CreateNetwork(ctx context.Context, connID string, config *NetworkConfig) (*NetworkInfo, error)
	DeleteNetwork(ctx context.Context, connID, networkName string) error

	// 网络操作
	StartNetwork(ctx context.Context, connID, networkName string) error
	StopNetwork(ctx context.Context, connID, networkName string) error
	SetAutostart(ctx context.Context, connID, networkName string, autostart bool) error

	// DHCP 租约管理
	GetDHCPLeases(ctx context.Context, connID, networkName string) ([]*DHCPLease, error)
}

// NetworkInfo 网络信息
type NetworkInfo struct {
	Name       string      `json:"name"`
	UUID       string      `json:"uuid"`
	Active     bool        `json:"active"`
	Persistent bool        `json:"persistent"`
	Autostart  bool        `json:"autostart"`
	Bridge     string      `json:"bridge"`
	Forward    ForwardMode `json:"forward"`
	IP         []NetworkIP `json:"ip"`
	Domain     string      `json:"domain"`
	CreatedAt  time.Time   `json:"created_at"`
}

type ForwardMode string

const (
	ForwardNone   ForwardMode = "none"   // 隔离网络
	ForwardNAT    ForwardMode = "nat"    // NAT 模式
	ForwardRoute  ForwardMode = "route"  // 路由模式
	ForwardBridge ForwardMode = "bridge" // 桥接模式
)

type NetworkIP struct {
	Address string       `json:"address"`
	Netmask string       `json:"netmask"`
	DHCP    *NetworkDHCP `json:"dhcp,omitempty"`
}

type NetworkDHCP struct {
	Start string     `json:"start"`
	End   string     `json:"end"`
	Hosts []DHCPHost `json:"hosts,omitempty"`
}

type DHCPHost struct {
	MAC  string `json:"mac"`
	Name string `json:"name"`
	IP   string `json:"ip"`
}

// NetworkConfig 网络创建配置
type NetworkConfig struct {
	Name      string            `json:"name" binding:"required"`
	Bridge    string            `json:"bridge"`
	Forward   ForwardMode       `json:"forward"`
	Domain    string            `json:"domain"`
	IP        []NetworkIPConfig `json:"ip"`
	Autostart bool              `json:"autostart"`
}

// NetworkInterfaceConfig 网络接口配置
type NetworkInterfaceConfig struct {
	Type   string `json:"type"`   // network, bridge
	Source string `json:"source"` // 网络名称或桥接名
	Model  string `json:"model"`  // virtio, e1000
	MAC    string `json:"mac"`    // MAC 地址（可选）
}

type NetworkIPConfig struct {
	Address string             `json:"address" binding:"required"`
	Netmask string             `json:"netmask" binding:"required"`
	DHCP    *NetworkDHCPConfig `json:"dhcp,omitempty"`
}

type NetworkDHCPConfig struct {
	Start string     `json:"start" binding:"required"`
	End   string     `json:"end" binding:"required"`
	Hosts []DHCPHost `json:"hosts,omitempty"`
}

// DHCPLease DHCP 租约
type DHCPLease struct {
	IPAddress  string    `json:"ip_address"`
	MACAddress string    `json:"mac_address"`
	Hostname   string    `json:"hostname"`
	ExpiryTime time.Time `json:"expiry_time"`
	Type       string    `json:"type"` // ipv4, ipv6
}
